package cs236369.hw5.servlet.listeners;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.Logger;

import cs236369.hw5.axis2.ypages.YellowPagesClient;
import cs236369.hw5.axis2.ypages.YellowPagesConf;

/**
 * this listener will load register to the yellow pages, when tomcat starts,
 * and unregister when tomcat stops.
 * 
 * @author Dror Baum
 * @author Amir Hasson
 * 
 */
public class YellowPagesServletListener implements ServletContextListener
{

	private final YellowPagesClient client = new YellowPagesClient();

	@Override
	public void contextInitialized(ServletContextEvent event)
	{
		Logger logger = Logger.getLogger(getClass());
		try
		{
			logger.info("loading yellow pages properties file");
			YellowPagesConf.initialize();
			logger.info("registering to yellow pages");
			client.registerToService();
		}
		catch (Exception e)
		{
			logger.error("Exception", e);
		}

	}

	@Override
	public void contextDestroyed(ServletContextEvent event)
	{
		Logger logger = Logger.getLogger(getClass());
		try
		{
			logger.info("unregistering from yellow pages");
			client.unregisterFromService();
		}
		catch (Exception e)
		{
			logger.error("Exception", e);
		}
	}
}
